﻿using Npgsql.Replication.PgOutput.Messages;
using System.Windows;
using System.Windows.Input;
using YY.Admin.Core.FluentValidation;
using YY.Admin.ViewModels;
using Window = HandyControl.Controls.Window;
namespace YY.Admin.Views
{
    /// <summary>
    /// LoginWindow.xaml 的交互逻辑
    /// </summary>
    public partial class LoginWindow : Window
    {
        public LoginWindow()
        {
            InitializeComponent();
            //this.Closed += LoginWindow_Closed;
            FluentValidationHelper.Attach(RootPanel, typeof(LoginWindowViewModel));
        }

        private void LoginWindow_Closed(object? sender, EventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                Submit_Click(sender, e);
            }
        }


        private void Submit_Click(object sender, RoutedEventArgs e)
        {
            var vm = (LoginWindowViewModel)DataContext;
            vm.LoginMessage = string.Empty;
            bool valid = FluentValidationHelper.ValidateAll(RootPanel, vm.LoginInput!, vm.LoginInputValidator);
            if (valid)
            {
                if (vm.LoginCommand.CanExecute())
                {
                    vm.LoginCommand.Execute();
                }
            }
            // 阻止事件继续冒泡
            e.Handled = true;
        }
    }
}
